package com.cnap.db.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * 将list转为字符串的handler
 *
 * @author xc
 **/
public class ListStrHandler implements TypeHandler<List<String>> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
        String str = String.join(",", strings);
        preparedStatement.setString(i, str);
    }

    @Override
    public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
        String[] strings = resultSet.getString(s).split(",");
        return Arrays.asList(strings);
    }

    @Override
    public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
        String[] strings = resultSet.getString(i).split(",");
        return Arrays.asList(strings);
    }

    @Override
    public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
        String[] strings = callableStatement.getString(i).split(",");
        return Arrays.asList(strings);
    }

}
